package com.tozed.android.launcher.simpleui;

import java.util.ArrayList;
import java.util.List;

import com.tozed.android.launcher.R;

import android.content.Context;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddAppViewPager extends ViewPager
{
	private static final String TAG = "AddAppViewPager";
	
	private static final String ALL_APP_TAG_FLAG = "all apps";
	private static final String ALL_DOWN_LOAD_TAG_FLAG = "download apps";
	private static final int PAGE_ALL_APP = 0;
	private static final int PAGE_DOWNLOAD = 1;
	private static final long UPDATE_LAYOUT_DELAY = 1000L;
	private boolean canScroll;
	private AllAppScrollView mAllAppScrollView;
	private Context mContext;
	private DownLoadAppView mDownLoadAppView;
	private LayoutInflater mInflater;
	private int[] mLayouts = { R.layout.simple_all_apps, R.layout.simple_download_apps };
	private List<View> mPagerList;
	private CharSequence mSearchWidgetText;
	
	public AddAppViewPager(Context context){
		this(context, null);
	}
	
	public AddAppViewPager(Context context, AttributeSet attrs)
	{
		super(context, attrs);
	    mContext = context;
	    init();
	}
	
	private void init()
	{
		Log.i(TAG, "AddAppViewPagerinit");
		
		mInflater = ((LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE));
		mPagerList = new ArrayList<View>();
		
		for (int i = 0; i < mLayouts.length; i++)
			mPagerList.add(mInflater.inflate(mLayouts[i], null));
		setAdapter(new AddAppPagerAdapter());
		
		mAllAppScrollView = ((AllAppScrollView) findScrollView(ALL_APP_TAG_FLAG));
		mDownLoadAppView = ((DownLoadAppView) findScrollView(ALL_DOWN_LOAD_TAG_FLAG));
	}
	
	public View findScrollView(String strName){
		if (strName != null)
		{
			LinearLayout pageView = null;
			if (strName.equals(ALL_APP_TAG_FLAG))
			{
				pageView = (LinearLayout)mPagerList.get(PAGE_ALL_APP);
				if (pageView != null){
					mAllAppScrollView = ((AllAppScrollView)pageView.findViewById(R.id.all_apps_scroll_view));
			        return mAllAppScrollView;
				}else{
					if ((mAllAppScrollView != null))
			            return mAllAppScrollView;
				}
				
			}else{
				pageView = (LinearLayout)mPagerList.get(PAGE_DOWNLOAD);
				if (pageView != null){
			        mDownLoadAppView = ((DownLoadAppView)pageView);
				    return mDownLoadAppView;
				}else{
					if (mDownLoadAppView != null)
			            return mDownLoadAppView;
				}
			}
		}
		
		return null;
	}
	
	public boolean onInterceptTouchEvent(MotionEvent event) 
	{
		canScroll = false;
	    return super.onInterceptTouchEvent(event);
	}
	
	@Override
	protected void onPageScrolled(int arg0, float arg1, int arg2){
		super.onPageScrolled(arg0, arg1, arg2);
	}
	
	@Override
	public void scrollTo(int x, int y)
	{
		if (!canScroll)
		     return;
		
		super.scrollTo(x, y);
	}
	
	public void updateCurrentPage(final SimpleApplicationInfo info, final boolean removed)
	{
		new Handler().postDelayed(new Runnable(){
			@Override
			public void run()
			{
				if (mAllAppScrollView != null)
					mAllAppScrollView.updateCurrentView(info, removed);
				if (mDownLoadAppView != null)
					mDownLoadAppView.updateDownLoadApp(info, removed);
			}
		}, 1000);
	}
	
	public void updateAllAppsViewData()
	{
		if (mAllAppScrollView != null)
		    mAllAppScrollView.updateAllAppsViewData();
	}

	class AddAppPagerAdapter extends PagerAdapter
	{
		AddAppPagerAdapter(){
	    }
		
		// 销毁
		public void destroyItem(View paramView, int paramInt, Object paramObject){
			((ViewPager) paramView).removeView((View)mPagerList.get(paramInt));
		}
		
		// 实例化视图
		public Object instantiateItem(View paramView, int paramInt)
	    {
	      ((ViewPager)paramView).addView((View)mPagerList.get(paramInt));
	      return mPagerList.get(paramInt);
	    }
		
		@Override
		public int getCount(){
			return mPagerList.size();
		}

		@Override
		public boolean isViewFromObject(View view, Object obj){
			return view == obj;
		}
	}

	public void changePage(String strName)
	{
		canScroll = true;
		
		if (strName == null)
			return;
		
	    if (strName.equals(ALL_APP_TAG_FLAG))
	    {
	      setCurrentItem(PAGE_ALL_APP, false);
	      if (mAllAppScrollView != null && mSearchWidgetText != null && mSearchWidgetText.length() != 0)
	    	  mAllAppScrollView.setSearchWidgetText(mSearchWidgetText);
	    }else{
			setCurrentItem(PAGE_DOWNLOAD, false);
			if (mAllAppScrollView != null){
				mSearchWidgetText = mAllAppScrollView.clearSearchWidget();
			}
	    }
	    
	    if (mDownLoadAppView != null){
	    	TextView emptyView = (TextView)findViewById(R.id.no_download_apps);
	    	mDownLoadAppView.checkIsEmpty(emptyView);
	    }
	}

	public void updateChildContext(Context context)
	{
		if (mAllAppScrollView == null)
			mAllAppScrollView = ((AllAppScrollView)findScrollView(ALL_APP_TAG_FLAG));
		mAllAppScrollView.setContext(context);
		
		if (mDownLoadAppView == null)
			mDownLoadAppView = ((DownLoadAppView)findScrollView(ALL_DOWN_LOAD_TAG_FLAG));
		mDownLoadAppView.setContext(context);
	}

	public void updateRecentView()
	{
		if (mAllAppScrollView != null)
			mAllAppScrollView.updateRecentApps();
	}

	public void clearSearchText()
	{
		mAllAppScrollView.clearSearchWidget();
		mSearchWidgetText = null;
	}
	
}
